using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class getDoorDown : MonoBehaviour
{
    public Rigidbody2D rb2d;
    public GameObject rope;
    public AudioSource audiosource;
    // Start is called before the first frame update


    public void OnTriggerEnter2D(Collider2D collision)
    {
        if (rb2d.bodyType == RigidbodyType2D.Static)
        {
            audiosource.Play();
            rb2d.bodyType = RigidbodyType2D.Dynamic;
            rope.GetComponent<SpriteRenderer>().enabled = false;
            StartCoroutine(ChangeColor(Color.red, true));
        }
    }

    IEnumerator ChangeColor(Color color, bool up)
    {
        Vector3 deltaV;
        if (up)
        {
            deltaV = new Vector3(0, 0.2f, 0);
        }
        else
        {
            deltaV = new Vector3(0, -0.2f, 0);
        }
        SpriteRenderer sr = GetComponent<SpriteRenderer>();
        float time = 0;
        Color deltaColor = color - sr.color;
        while (time < 0.5f)
        {
            time += Time.deltaTime;
            transform.position += deltaV * Time.deltaTime;
            sr.color += deltaColor * (Time.deltaTime / 0.5f);
            yield return null;
        }
        sr.color = color;
    }
}
